package com.qlslylq.ad.example.activity;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.qlslylq.ad.example.R;
import com.qlslylq.ad.example.constant.RequestCode;
import com.qlslylq.ad.example.manager.PermissionManager;
import com.qlslylq.ad.sdk.config.AdDesignHolder;
import com.qlslylq.ad.sdk.enums.AdPlatformEnum;
import com.qlslylq.ad.sdk.util.Log;

/**
 * 主页<br/>
 */
public class MainActivity extends BaseActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    protected void initView() {
        setTitle(getString(R.string.app_name));
    }

    protected void initData() {
        checkNeedPermission();
    }

    protected void initListener() {
        setOnCheckedChangeListener();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_0:
                startActivity(SplashAdTestMainActivity.class);
                break;
            case R.id.btn_1:
                startActivity(InterstitialAdTestMainActivity.class);
                break;
            case R.id.btn_2:
                startActivity(FullScreenAdTestMainActivity.class);
                break;
            case R.id.btn_3:
                startActivity(RewardAdTestMainActivity.class);
                break;
            case R.id.btn_4:
                startActivity(BannerAdTestMainActivity.class);
                break;
            case R.id.btn_5:
                startActivity(FeedAdTestMainActivity.class);
                break;
        }
    }

    /**
     * 启用或禁用平台调试，仅用于SDK内部开发人员临时调试用，app端请不要调用enablePlatform函数<br/>
     */
    private void setOnCheckedChangeListener() {
        LinearLayout layout_check = findView(R.id.layout_check);
        for (int index = 0; index < layout_check.getChildCount(); index++) {
            CheckBox cb = (CheckBox) layout_check.getChildAt(index);
            cb.setOnCheckedChangeListener((btn, checked) -> {
                switch (btn.getId()) {
                    case R.id.cb_ad_provider_csj:
                        AdDesignHolder.enablePlatform(AdPlatformEnum.PLATFORM_CSJ, checked);
                        break;
                    case R.id.cb_ad_provider_ylh:
                        AdDesignHolder.enablePlatform(AdPlatformEnum.PLATFORM_YLH, checked);
                        break;
                    case R.id.cb_ad_provider_ks:
                        AdDesignHolder.enablePlatform(AdPlatformEnum.PLATFORM_KS, checked);
                        break;
                    case R.id.cb_ad_provider_sigmob:
                        AdDesignHolder.enablePlatform(AdPlatformEnum.PLATFORM_SIGMOB, checked);
                        break;
                    case R.id.cb_ad_provider_mintegral:
                        AdDesignHolder.enablePlatform(AdPlatformEnum.PLATFORM_MINTEGRAL, checked);
                        break;
                }
            });
        }
    }

    /**
     * 申请动态权限[ACCESS_FINE_LOCATION，ACCESS_COARSE_LOCATION，READ_PHONE_STATE，READ_EXTERNAL_STORAGE，WRITE_EXTERNAL_STORAGE]<br/>
     * 获取上述权限是为了更好地推荐个性化广告，俱为可选权限，拒绝后由开发者决定是否继续申请<br/>
     */
    private void checkNeedPermission() {
        PermissionManager.checkNeedPermission(this);
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == RequestCode.REQUEST_CODE_NEED) {
            boolean isAllGranted = true;
            for (int grant : grantResults) {
                if (grant != PackageManager.PERMISSION_GRANTED) {
                    isAllGranted = false;
                    break;
                }
            }
            Log.d(String.format("是否已同意所有权限申请：%s", isAllGranted));
        }
    }

}